#!/bin/bash
numpass=0

#Location of server and client
lserver=./server
lclient=./client

#Server ip address/prt
serverip=127.0.0.1
serverprt=10500

badsprt=10701
serverin=serverin.txt

#Test client I/O
client1in=client1in.txt
client1out=client1out.txt
client1compare=client1compare.txt

#Provided client I/O
client2in=client2in.txt
client2out=client2out.txt
client2compare=client2compare.txt

function cleanup {
    echo "Cleaning up... please wait"
    echo >> $serverin
    echo "exit" >> $client1in
    echo "exit" >> $client2in
    sleep 1
    rm *.txt
    exit
}

#Start server
echo -n > $serverin
($lserver $serverprt < $serverin > /dev/null)&

####################################################################
#Obtain test client location and start client
####################################################################
echo -ne "Enter path (complete or relative) of client:"
read tclient
echo "There are 27 tests"

#Check connect0
echo -n > $client1in
echo -n > $client1out

($tclient $serverip $badsprt < $client1in 2> $client1out)&
echo "connect: Connection refused" >> $client1compare
sleep 1
if diff $client1compare $client1out > /dev/null
then
    let numpass=numpass+1
    echo "Passed $numpass"
else
    echo "Tested client with incorrect port"
    echo "Expected:"
    echo "connect: Connection refused"
    cleanup
fi

#Check connect1
rm $client1in $client1out $client1compare
echo -n > $client1in
echo -n > $client1out

($tclient $serverip $serverprt < $client1in > $client1out)&
echo -ne "Connected to server. Type exit to quit\n>" >> $client1compare
sleep 1
if diff $client1compare $client1out > /dev/null
then
    let numpass=numpass+1
    echo "Passed $numpass"
else
    echo "Tested client with correct command-line args"
    echo "Expected:"
    echo -e "Connected to server. Type exit to quit\n>"
    cleanup
fi

####################################################################
# Client Input checks
####################################################################

#Check login0
echo "login" >> $client1in
echo -ne "Bad username\n>" >> $client1compare
sleep 1
if diff $client1compare $client1out > /dev/null
then
    let numpass=numpass+1
    echo "Passed $numpass"
else
    echo "Tested login with no params"
    echo "Expected:"
    echo  -e "Bad username\n>"
    cleanup
fi

#Check login1
echo "login Jimmy Dean" >> $client1in
echo -ne "Bad username\n>" >> $client1compare
sleep 1
if diff $client1compare $client1out > /dev/null
then
    let numpass=numpass+1
    echo "Passed $numpass"
else
    echo "Tested login with username containing space"
    echo "Expected:"
    echo -e "Bad username\n>"
    cleanup
fi

#Check login2
echo "login abababababababababababab" >> $client1in
echo -ne "Bad username\n>" >> $client1compare
sleep 1
if diff $client1compare $client1out > /dev/null
then 
    let numpass=numpass+1
    echo "Passed $numpass"
    
else 
   echo "Tested:login with screen name > 20 characters"
   echo "Expected:"
   echo  -e "Bad username\n>"
   cleanup
fi

#Check sendto0
echo "sendto" >> $client1in
echo -ne "Bad username\n>" >> $client1compare
sleep 1
if diff $client1compare $client1out > /dev/null
then
    let numpass=numpass+1
    echo "Passed $numpass"
else
    echo "Tested sendto with no params"
    echo "Expected:"
    echo -e "Bad username\n>"
    cleanup
fi

#Check sendto1
echo "sendto bob" >> $client1in
echo -ne "Bad username\n>" >> $client1compare
sleep 1
if diff $client1compare $client1out > /dev/null
then
    let numpass=numpass+1
    echo "Passed $numpass"
else
    echo "Tested sendto with one param"
    echo "Expected:"
    echo -e "Bad username\n>"
    cleanup
fi

#Check sendto2
echo "sendto abababababababababababab happy" >> $client1in
echo -ne "Bad username\n>" >> $client1compare
sleep 1
if diff $client1compare $client1out > /dev/null
then
    let numpass=numpass+1
    echo "Passed $numpass"
else
    echo "Tested sendto with username > 20 characters"
    echo "Expected:"
    echo -e "Bad username\n>"
    cleanup
fi

#Check list0
echo "list bob" >> $client1in
echo -ne "Invalid command\n>" >> $client1compare
sleep 1
if diff $client1compare $client1out > /dev/null
then
    let numpass=numpass+1
    echo "Passed $numpass"
else
    echo "Tested list with an argument"
    echo "Expected:"
    echo -e "Invalid command\n>"
    cleanup
fi

#Check logout0
echo "logout bob" >> $client1in
echo -ne "Invalid command\n>" >> $client1compare
sleep 1
if diff $client1compare $client1out > /dev/null
then
    let numpass=numpass+1
    echo "Passed $numpass"
else
    echo "Tested logout with an argument"
    echo "Expected:"
    echo -e "Invalid command\n>"
    cleanup
fi

#Check exit0
echo "exit fred" >> $client1in
echo -ne "Invalid command\n>" >> $client1compare
sleep 1
if diff $client1compare $client1out > /dev/null
then
    let numpass=numpass+1
    echo "Passed $numpass"
else
    echo "Tested exit with an argument"
    echo "Expected:"
    echo -e "Invalid command\n>"
    cleanup
fi

##############################################################
#Client - server interactions
##############################################################

#Check empty list
echo "list" >> $client1in
echo -ne "<end list>\n>" >> $client1compare
sleep 1
if diff $client1compare $client1out > /dev/null
then
    let numpass=numpass+1
    echo "Passed $numpass"
else
    echo "Tested list when no one is logged in"
    echo "Expected:"
    echo -e "<end list>\n>"
    cleanup
fi

#Start provided client
echo -n > $client2in
echo -n > $client2out
($lclient $serverip $serverprt < $client2in > $client2out)&

#Login as fred with provided client
echo "login fred" >> $client2in
echo -ne "Connected to server. Type exit to quit\n>" >> $client2compare
echo -ne "Success\n>">> $client2compare
sleep 1

#Check conflicting login
echo "login fred" >> $client1in
echo -ne "Failure\n>" >> $client1compare
sleep 1
if diff $client1compare $client1out > /dev/null
then
    let numpass=numpass+1
    echo "Passed $numpass"
else
    echo "Tested login with conflicting username"
    echo "Expected:"
    echo -e "Failure\n>"
    cleanup
fi

#Check 1 entry list
echo "list" >> $client1in
echo -ne "fred\n<end list>\n>" >> $client1compare
sleep 1
if diff $client1compare $client1out > /dev/null
then 
    let numpass=numpass+1
    echo "Passed $numpass"
else
    echo "Tested list when fred is logged in"
    echo "Expected:"
    echo -e "fred\n<end list>\n>"
    cleanup
fi

#Check successful login
echo "login bob" >> $client1in
echo -ne "Success\n>">> $client1compare
sleep 1
if diff $client1compare $client1out > /dev/null
then
    let numpass=numpass+1
    echo "Passed $numpass"
else
    echo "Tested login with valid, non-conflicting username"
    echo "Expected:"
    echo -e "Success\n>"
    cleanup
fi

#Check login while already logged in
echo "login joe" >> $client1in
echo -ne "Failure\n>">> $client1compare
sleep 1
if diff $client1compare $client1out > /dev/null
then
    let numpass=numpass+1
    echo "Passed $numpass"
else
    echo "Tested login when already logged in"
    echo "Expected:"
    echo -e "Failure\n>"
    cleanup
fi

#Check list with 2 elements
echo "list" >> $client1in
echo -ne "fred\nbob\n<end list>\n>" >> $client1compare
sleep 1
if diff $client1compare $client1out > /dev/null
then
    let numpass=numpass+1
    echo "Passed $numpass"
else
    echo "Tested list when fred and bob are logged in"
    echo "Expected:"
    echo -e "fred\nbob\n<end list>\n>"
    cleanup
fi

#Check invalid sendto
echo "sendto joe hi" >> $client1in
echo -ne "Failure\n>" >> $client1compare
sleep 1
if diff $client1compare $client1out > /dev/null
then
    let numpass=numpass+1
    echo "Passed $numpass"
else
    echo "Tested:sendto to non-existent username"
    echo "Expected:"
    echo -e "Failure\n>"
    cleanup
fi

#Check valid sendto - part 1
echo "sendto fred hey, how are you doing?" >> $client1in
echo -ne "Success\n>" >> $client1compare
sleep 1
if diff $client1compare $client1out > /dev/null
then
    let numpass=numpass+1
    echo "Passed $numpass"
else
    echo "Tested sending valid sendto with long message"
    echo "Expected:"
    echo -e "Success\n>"
    cleanup
fi

#Check valid sendto - part2
echo -ne "bob:hey, how are you doing?\n>" >> $client2compare
sleep 1
if diff $client2compare $client2out > /dev/null
then
    let numpass=numpass+1
    echo "Passed $numpass"
else
    echo "Tested sending valid sendto with long message"
    echo "Error: Destination received invalid message"
    cleanup
fi

#Check valid sendto - part 3
echo "sendto fred hello?" >> $client1in
echo -ne "Success\n>" >> $client1compare
sleep 1
if diff $client1compare $client1out > /dev/null
then
    let numpass=numpass+1
    echo "Passed $numpass"
else
    echo "Tested sending valid sendto with short message"
    echo "Expected:"
    echo -e "Success\n>"
    cleanup
fi

#Check valid sendto - part4
echo -ne "bob:hello?\n>" >> $client2compare
sleep 1
if diff $client2compare $client2out > /dev/null
then
    let numpass=numpass+1
    echo "Passed $numpass"
else
    echo "Tested sending valid sendto with short message"
    echo "Error: Destination received invalid message"
    cleanup
fi

#Check valid sendto - part 5
echo "sendto bob hows life treating ya?" >> $client2in
echo -ne "fred:hows life treating ya?\n>" >> $client1compare
sleep 1
if diff $client1compare $client1out > /dev/null
then
    let numpass=numpass+1
    echo "Passed $numpass"
else
    echo "Tested receiving valid sendto with long message"
    echo "Expected:"
    echo -e "fred:hows life treating ya?\n>"
    cleanup
fi

#Check valid sendto - part 6
echo "sendto bob hi!" >> $client2in
echo -ne "fred:hi!\n>" >> $client1compare
sleep 1
if diff $client1compare $client1out > /dev/null
then
    let numpass=numpass+1
    echo "Passed $numpass"
else
    echo "Tested receiving valid sendto with short message"
    echo "Expected"
    echo -e "fred:hi!\n>"
    cleanup
fi

#Check valid logout - part 0
echo "logout" >> $client1in
echo -ne "Success\n>" >> $client1compare
sleep 1
if diff $client1compare $client1out > /dev/null
then
    let numpass=numpass+1
    echo "Passed $numpass"
else
    echo "Tested logout"
    echo "Expected:"
    echo -e "Success\n>"
    cleanup
fi

#Check valid logout - part 1
echo "logout" >> $client1in
echo -ne "Failure\n>" >> $client1compare
sleep 1
if diff $client1compare $client1out > /dev/null
then
    let numpass=numpass+1
    echo "Passed $numpass"
else
    echo "Tested logout when already logged out"
    echo "Expected:"
    echo -e "Failure\n>"
    cleanup
fi

#Check for relogin
echo "login joe" >> $client1in
echo -ne "Success\n>" >> $client1compare
sleep 1
if diff $client1compare $client1out > /dev/null
then
    let numpass=numpass+1
    echo "Passed $numpass"
else
    echo "Tested login after having logged out"
    echo "Expected:"
    echo -e "Success\n>"
    cleanup
fi

echo "Congratulations, you passed all the tests!"
cleanup
